/**
 * 
 */
package com.yidin.local;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.Projection;
import com.yidin.lsc.R;

/**
 * 百度地图
 * 
 * @author cola
 * 
 */
public class MyBaiduMap extends MapActivity {
	private BMapManager mBMapMan;
	public View popView;//地图点击的弹窗
    private MapView mapView;
    private MapController mMapController;
    private List<TextView> showTextArray = new ArrayList<TextView>();
    private MyOverItem myOverItem;//用来控件各个点
    private Button down_pop, up_pop;
    private int currentOver = 0;//当前显示的标签

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_map);

		mBMapMan = new BMapManager(getApplication());
		mBMapMan.init("D7E3959A574AC544C10485C2C998B5D45ACC9F7F", null);
		super.initMapActivity(mBMapMan);
		
		this.mapView = (MapView) findViewById(R.id.bmapsView);
		this.mapView.setBuiltInZoomControls(false); // 设置启用内置的缩放控件

		this.mMapController = this.mapView.getController(); // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		GeoPoint point = new GeoPoint((int) (23.315 * 1E6),
				(int) (113.404 * 1E6)); // 用给定的经纬度构造一个GeoPoint，单位是微度 (度 * 1E6)
		mMapController.setCenter(point); // 设置地图中心点
		mMapController.setZoom(12); // 设置地图zoom级别
		
		this.initPopview();
		this.initOverlayItem();
		
		// 添加定位图层
//		MyBaiduLocationOverlay mylocTest = new MyBaiduLocationOverlay(this, this.mapView,BitmapFactory.decodeResource(getResources(), R.drawable.where));
//		mylocTest.enableMyLocation(); // 启用定位
//		mylocTest.enableCompass();    // 启用指南针
//		this.mapView.getOverlays().add(mylocTest);
	}

    private void initOverlayItem(){
//    	Drawable marker = getResources().getDrawable(R.drawable.shop_icon);  //得到需要标在地图上的资源
//    	marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker
//				.getIntrinsicHeight());   //为maker定义位置和边界
//    	myOverItem = new MyOverItem(marker, this);
//		mapView.getOverlays().add(myOverItem); //添加ItemizedOverlay实例到mMapView
    }
    
	private OnClickListener zoomUpListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if(currentOver == -1)
				currentOver = 0;
			if (currentOver < myOverItem.size() - 1)
				myOverItem.showOverlayItem(currentOver++);
		}

	};

	private OnClickListener zoomDownListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if(currentOver == myOverItem.size())
				currentOver = myOverItem.size() - 1;
			if (currentOver >= 0)
				myOverItem.showOverlayItem(currentOver--);
		}

	};
	
	/**
	 *  由于气泡的尾巴是在下边居中的,因此要设置成MapView.LayoutParams.BOTTOM_CENTER.
     *   这里没有给GeoPoint,在onFocusChangeListener中设置
	 */
	private void initPopview() {
//		int len = this.getShopInfo().size();
//		for (int n = 0; n < len; n++) {
//			TextView tv2 = new TextView(this);
//			showTextArray.add(tv2);
//			mapView.addView(tv2, new MapView.LayoutParams(
//					MapView.LayoutParams.WRAP_CONTENT,
//					MapView.LayoutParams.WRAP_CONTENT, null,
//					MapView.LayoutParams.BOTTOM_CENTER));
//			tv2.setVisibility(View.GONE);
//		}
//
//		popView = super.getLayoutInflater().inflate(R.layout.pop, null);
//		mapView.addView(popView, new MapView.LayoutParams(
//				MapView.LayoutParams.WRAP_CONTENT,
//				MapView.LayoutParams.WRAP_CONTENT, null,
//				MapView.LayoutParams.BOTTOM_CENTER));
//		popView.setVisibility(View.GONE);
	}
	
	/**
	 * 多个overlay显示时通过ItemizedOverlay
	 * 
	 * @author cola
	 * 
	 */
	class MyOverItem extends ItemizedOverlay<OverlayItem> {
		private List<OverlayItem> GeoList = new ArrayList<OverlayItem>();
		private Drawable marker;
		private MyBaiduMap mContext;

		private TextView textView1;
		private TextView textView2;
		
		private int layout_x = 0; // 用于设置popview 相对某个位置向x轴偏移  
	    private int layout_y = -25; // 用于设置popview 相对某个位置向x轴偏移  

		public MyOverItem(Drawable marker, Context context) {
			super(boundCenterBottom(marker));
			populate();
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			// Projection接口用于屏幕像素点坐标系统和地球表面经纬度点坐标系统之间的变换
			Projection projection = mapView.getProjection();
			
			for (int index = size() - 1; index >= 0; index--) { // 遍历GeoList
				OverlayItem overLayItem = getItem(index); // 得到给定索引的item
				String title = overLayItem.getTitle();
				// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
				Point point = projection.toPixels(overLayItem.getPoint(), null); 

				Paint paintCircle = new Paint();
				paintCircle.setColor(Color.RED);
				canvas.drawCircle(point.x, point.y, 5, paintCircle); // 画圆

//				Paint paintText = new Paint();
//				paintText.setColor(Color.BLACK);
//				paintText.setTextSize(18);
//				canvas.drawText(title, point.x, point.y + 35, paintText); // 绘制文本

				TextView tv = mContext.showTextArray.get(index);
				MapView.LayoutParams geoLP = (MapView.LayoutParams) tv
						.getLayoutParams();
				geoLP.x = layout_x;//Y轴偏移  
				geoLP.y = layout_y + 30;//Y轴偏移 
				tv.setTextColor(Color.WHITE);
				tv.setText(index + "");
				tv.setTextSize(12);
				tv.setVisibility(View.VISIBLE);
				geoLP.point = overLayItem.getPoint();
				mapView.updateViewLayout(mContext.showTextArray.get(index), geoLP);
			}
			
			super.draw(canvas, mapView, shadow);
			// 调整一个drawable边界，使得（0，0）是这个drawable底部最后一行中心的一个像素
			boundCenterBottom(marker);
		}

		@Override
		protected OverlayItem createItem(int i) {
			return GeoList.get(i);
		}

		@Override
		public int size() {
			return GeoList.size();
		}

		@Override
		/**
		 * 处理当点击事件
		 * mapview的onTouch事件会传播到overlay的 onTouch方法 通过点击范围可以确定触发哪个overlay的onTap
		 */
		protected boolean onTap(int i) {
			setFocus(GeoList.get(i)); 
			MapView.LayoutParams geoLP = (MapView.LayoutParams) mContext.popView
					.getLayoutParams();
			return true;
		}
		
		/**
		 * 显示相关的点
		 */
		public void showOverlayItem(int n){
			this.onTap(n);
		}
	}
	
	@Override
	protected void onDestroy() {
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (mBMapMan != null) {
			mBMapMan.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (mBMapMan != null) {
			mBMapMan.start();
		}
		super.onResume();
	}
}
